package com.ddzh.config;

import com.ddzh.security.JwtAuthenticationEntryPoint;
import com.ddzh.security.JwtAuthenticationTokenFilter;
import com.ddzh.security.MyAuthenticationFailureHandler;
import com.ddzh.security.MyAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

//@Configuration
//自定义配置权限
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Resource
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Resource
    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //主要是对一些静态资源的处理
        web.ignoring().antMatchers("/v2/api-docs",
                "/webjars/**",
                "/swagger-resources/configuration/ui",
                "/swagger-resources",
                "/swagger-resources/configuration/security",
                "/swagger-ui.html",
                "/js/**",
                "/css/**"
        );
    }

    /**
     * 配置授权，这一块配置是重点，对应着我们的过滤器链
     * */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //登录处理
        http.formLogin()
                //登入成功后的具体处理器 handler
                .successHandler(myAuthenticationSuccessHandler)
                //登入失败后的具体处理器 handler
                .failureHandler(myAuthenticationFailureHandler)
                .and()
                // 除了登入外接口请求异常处理
                .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
                .and()
            .authorizeRequests()
            .antMatchers("/login*").permitAll()
            //post请求多次的处理方法
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .csrf().disable();
        //在过滤器链中加入我们自己的过滤器链，用来处理登录之后前端发来的请求中携带的token
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
